package cn.mesmile.workflow.process.controller;

import cn.mesmile.workflow.commom.Query;
import cn.mesmile.workflow.commom.R;
import cn.mesmile.workflow.process.bo.OperationTaskBO;
import cn.mesmile.workflow.process.qo.WorkFlowAdminQO;
import cn.mesmile.workflow.process.service.WorkFlowAdminService;
import cn.mesmile.workflow.process.vo.DoneProcessVO;
import cn.mesmile.workflow.process.vo.TodoTaskVO;
import cn.mesmile.workflow.process.wrapper.DoneProcessWrapper;
import cn.mesmile.workflow.process.wrapper.TodoTaskWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.web.bind.annotation.*;

/**
 * @author zb
 * @Description
 */
@Api(value = "流程运维相关接口", tags = "流程运维相关接口")
@AllArgsConstructor
@RequestMapping("/api/v1/workflow/admin")
@RestController
public class WorkFlowAdminController {

    private final WorkFlowAdminService workFlowAdminService;


    @ApiOperation("查询 所有待办任务 列表")
    @GetMapping("/listAllTodoTask")
    public R listTodoTask(Query query, WorkFlowAdminQO workFlowAdminQO){
        IPage<Task> taskPage = workFlowAdminService.listAllTodoTask(query, workFlowAdminQO);
        IPage<TodoTaskVO> todoTaskVOPage = TodoTaskWrapper.build().pageVO(taskPage);
        return R.data(todoTaskVOPage);
    }

    @ApiOperation("查询 所有已经完成的流程实例 列表")
    @GetMapping("/listAllDoneTask")
    public R listAllDoneTask(Query query, WorkFlowAdminQO workFlowAdminQO){
        IPage<HistoricProcessInstance> historicProcessInstancePage = workFlowAdminService.listAllDoneTask(query, workFlowAdminQO);
        IPage<DoneProcessVO> doneProcessVOPage = DoneProcessWrapper.build().pageVO(historicProcessInstancePage);
        return R.data(doneProcessVOPage);
    }

    @ApiOperation("催办任务处理人，处理任务,这里是发送通知消息")
    @PostMapping("/urgeTask")
    public R urgeTask(@RequestBody OperationTaskBO operationTaskBO){
        workFlowAdminService.urgeTask(operationTaskBO);
        return R.status(true);
    }

    @ApiOperation("转办任务")
    @PostMapping("/transferTask")
    public R transferTask(@RequestBody OperationTaskBO operationTaskBO){
        workFlowAdminService.transferTask(operationTaskBO);
        return R.success("转办任务成功");
    }

    @ApiOperation("委托任务")
    @PostMapping("/delegateTask")
    public R delegateTask(@RequestBody OperationTaskBO operationTaskBO){
        workFlowAdminService.delegateTask(operationTaskBO);
        return R.success("委托任务成功");
    }

    @ApiOperation("终止任务,跳转流程到结束节点")
    @PostMapping("/terminationTask")
    public R  terminationTask (@RequestBody OperationTaskBO operationTaskBO){
        workFlowAdminService.terminationTask(operationTaskBO);
        return R.success("终止任务成功");
    }

    @ApiOperation("加签任务")
    @PostMapping("/addMultiInstance")
    public R addMultiInstance(@RequestBody OperationTaskBO operationTaskBO){
        workFlowAdminService.addMultiInstance(operationTaskBO);
        return R.success("加签任务成功");
    }

    @ApiOperation("减签任务")
    @PostMapping("/deleteMultiInstance")
    public R deleteMultiInstance(@RequestBody OperationTaskBO operationTaskBO){
        workFlowAdminService.deleteMultiInstance(operationTaskBO);
        return R.success("减签任务成功");
    }

    @ApiOperation("指定回退任务到指定节点")
    @PostMapping("/rollbackTask")
    public R rollbackTask(@RequestBody OperationTaskBO operationTaskBO){
        workFlowAdminService.rollbackTask(operationTaskBO);
        return R.success("回退任务到指定节点成功");
    }




}
